package com.ylc.chat.JUI;

import com.ylc.chat.client.ClientDataBuffer;
import com.ylc.chat.client.ClientUtil;
import com.ylc.chat.common.Request;
import com.ylc.chat.common.Response;
import com.ylc.chat.common.ResponseStatus;
import com.ylc.chat.entity.User;
import com.ylc.chat.service.UserService;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.DataBuffer;
import java.io.IOException;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
/**
 * @Author: ylc
 * @Description:注册界面
 */
public class RegisterFrame extends JFrame {

	private JPanel contentPane;
	private final JLabel lblNewLabel_1 = new JLabel("账号：");
	private JPasswordField passwordField;
	private JTextField textField;
	private JPasswordField passwordField_1;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegisterFrame frame = new RegisterFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RegisterFrame() {
		this.init();
		setVisible(true);

	}

	private void init(){
		setBackground(new Color(255, 255, 255));
		setTitle("注册界面");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 255, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);


		JLabel lblNewLabel = new JLabel("多人聊天室");

		lblNewLabel.setBounds(5, 5, 426, 32);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("华文新魏", Font.PLAIN, 30));
		contentPane.add(lblNewLabel);
		lblNewLabel_1.setFont(new Font("隶书", Font.PLAIN, 19));
		lblNewLabel_1.setBackground(new Color(0, 0, 0));
		lblNewLabel_1.setBounds(86, 58, 64, 49);
		contentPane.add(lblNewLabel_1);

		JLabel lblNewLabel_1_1 = new JLabel("密码：");
		lblNewLabel_1_1.setFont(new Font("隶书", Font.PLAIN, 19));
		lblNewLabel_1_1.setBackground(Color.BLACK);
		lblNewLabel_1_1.setBounds(86, 89, 64, 49);
		contentPane.add(lblNewLabel_1_1);

		passwordField = new JPasswordField();
		passwordField.setBounds(144, 133, 186, 21);
		contentPane.add(passwordField);

		JButton btnNewButton_1 = new JButton("注册");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				register();
			}
		});
		btnNewButton_1.setForeground(Color.BLACK);
		btnNewButton_1.setBackground(Color.WHITE);
		btnNewButton_1.setBounds(144, 192, 64, 23);
		contentPane.add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("重置");
		btnNewButton_2.setForeground(Color.BLACK);
		btnNewButton_2.setBackground(Color.WHITE);
		btnNewButton_2.setBounds(240, 192, 64, 23);
		contentPane.add(btnNewButton_2);

		textField = new JTextField();
		textField.setBounds(143, 74, 187, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(144, 105, 186, 21);
		contentPane.add(passwordField_1);

		JLabel lblNewLabel_1_1_1 = new JLabel("确认密码：");
		lblNewLabel_1_1_1.setFont(new Font("隶书", Font.PLAIN, 19));
		lblNewLabel_1_1_1.setBackground(Color.BLACK);
		lblNewLabel_1_1_1.setBounds(46, 117, 104, 49);
		contentPane.add(lblNewLabel_1_1_1);

		//关闭窗口
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				Request req = new Request();
				req.setAction("exit");
				try {
					ClientUtil.sendTextRequest(req);
				} catch (IOException ex) {
					ex.printStackTrace();
				}finally{
					System.exit(0);
				}
			}
		});
	}

		UserService userService = new UserService();
	public void register(){
		String uname = textField.getText();
		String password = new String(passwordField.getPassword());
		String validate = new String(passwordField_1.getPassword());

		if("".equals(uname)){
			JOptionPane.showMessageDialog(null,"账号不能为空！");
			return ;
		}
		if("".equals(password)){
			JOptionPane.showMessageDialog(null,"密码不能为空");
			return ;
		}
		if(!password.equals(validate)){
			JOptionPane.showMessageDialog(null,"两次密码输入不一致");
			return ;
		}
		// 重名校验
		if(userService.isSameName(uname) > 0){
			JOptionPane.showMessageDialog(null,"用户名重复");
			return ;
		}

		ClientDataBuffer.currentUser.setUname(uname);
		ClientDataBuffer.currentUser.setUpassword(password);

		// 完成注册
		Request req = new Request();
		req.setAction("userRegister");
		req.setAttribute("uname", uname);
		req.setAttribute("password", password);

		//获取响应
		Response response = null;
		try {
			response = ClientUtil.sendTextRequest(req);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		if(response.getStatus() == ResponseStatus.OK) {
			JOptionPane.showMessageDialog(null,"注册成功！");
			// 跳转聊天界面
			this.dispose();
			ChatFrame cFrame = new ChatFrame();
			cFrame.setVisible(true);
			// 修改用户状态为上线
			userService.updateOnline(ClientDataBuffer.currentUser.getUname(),1);
		}else {
			JOptionPane.showMessageDialog(null,"注册失败，请联系管理员！");
		}

	}
}
